package com.ronri.ams.task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.ronri.ams.bean.taskListBean;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;



/**
 * Created by Administrator on 2017/5/7.
 */

public class SimpleAdapterWithEvent extends BaseAdapter {

    // 外部接口事件
    private OnControlClickEvent mEvents = null;

    // 需要被监控的ID
    private int[] mMonitorID;

    // 句柄
    private Context mContext = null;

    private taskListBean mtaskListBean = null;

    private int mLayoutId = R.layout.v_task_detailed_list_item;


    /***
     * @param context
     * @param events
     * @param onEvent
     */
    public SimpleAdapterWithEvent(Context context, int layoutid, taskListBean data, int[] events, OnControlClickEvent onEvent) {
        mContext = context;
        mMonitorID = events;
        mEvents = onEvent;
        mtaskListBean = data;
        mLayoutId = layoutid;
    }

    @Override
    public int getCount() {
        if (mtaskListBean == null)
            return 0;

        if (mtaskListBean.getData() == null)
            return 0;

        if (mtaskListBean.getData().getRows() == null)
            return 0;

        return mtaskListBean.getData().getRows().size();
    }

    @Override
    public Object getItem(int position) {
        if (mtaskListBean == null)
            return null;

        if (mtaskListBean.getData() == null)
            return null;

        if (mtaskListBean.getData().getRows() == null)
            return null;

        return mtaskListBean.getData().getRows().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

            //对于listview，注意添加这一行，即可在item上使用高度
            AutoUtils.autoSize(convertView);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mtaskListBean == null)
            return convertView;

        if (mtaskListBean.getData() == null)
            return convertView;

        if (mtaskListBean.getData().getRows() == null)
            return convertView;

        if (mMonitorID.length > 0) {
            taskListBean.DataBean.RowsBean bean = mtaskListBean.getData().getRows().get(position);

            holder.tvTaskDate.setText(bean.getEnd_date());
            holder.tvTaskDeptName.setText(bean.getDept_name());
            holder.tvTaskNature.setText(bean.getSection_name());
            holder.tvTaskRegionName.setText(bean.getRegion_name());
            holder.tvTaskSampleName.setText(bean.getBreed_name());
            holder.tvTaskTitle.setText(bean.getTask_name());

            TextView talready = (TextView)convertView.findViewById(R.id.bt_task_already);
            TextView twait = (TextView)convertView.findViewById(R.id.bt_task_wait);

            if(talready != null){
                talready.setText("已完成(" + bean.getComplete_num() + ")");
            }

            if (twait != null){
                // wait
                twait.setText("待上传(" + bean.GetWait() + ")");
            }

            for (int i = 0; i < mMonitorID.length; i++) {
                View iTem = convertView.findViewById(mMonitorID[i]);
                iTem.setTag(position);
                iTem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mEvents != null) {
                            mEvents.OnClick(v, v.getId());
                            notifyDataSetChanged();
                        }
                    }
                });
            }
        }

        return convertView;
    }

    class ViewHolder {

        @BindView(R.id.tvTaskTitle)
        TextView tvTaskTitle;
        @BindView(R.id.tvTaskDeptName)
        TextView tvTaskDeptName;
        @BindView(R.id.textView)
        TextView textView;
        @BindView(R.id.tvTaskDate)
        TextView tvTaskDate;
        @BindView(R.id.tvTaskRegionName)
        TextView tvTaskRegionName;
        @BindView(R.id.tvTaskNature)
        TextView tvTaskNature;
        @BindView(R.id.tvTaskSampleName)
        TextView tvTaskSampleName;
//        @Bind(R.id.bt_do_sample)
//        TextView btDoSample;
//        @Bind(R.id.bt_task_already)
//        TextView btTaskAlready;
//        @Bind(R.id.bt_task_wait)
//        TextView btTaskWait;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
